package com.dsteiner.xelavos.client;

import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.widgets.client.RoundBorder;
import com.dsteiner.widgets.client.RoundBorder.RoundBorderModel;
import com.dsteiner.widgets.client.Widgets.FlowPanelExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.SimplePanelExt;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.domain.Sort;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.resources.RoundBorderResources;
import com.dsteiner.xelavos.client.controller.DashletController;
import com.dsteiner.xelavos.client.domain.Job;
import com.dsteiner.xelavos.client.domain.Leisure;
import com.dsteiner.xelavos.client.domain.Product;
import com.dsteiner.xelavos.client.domain.Service;
import com.dsteiner.xelavos.client.event.CreateObjectEvent;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.dsteiner.xelavos.client.model.SearchModel;
import com.dsteiner.xelavos.client.model.MainTabModel.Tab;
import com.dsteiner.xelavos.client.resources.XelavosImageBundle;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class DashletWidget extends LazyWidget {

	private class CreateObjectLink extends InlineHTML{
		public CreateObjectLink(final String text, final ModelObject modelObjectToCreate, final Tab tab) {
			super("<a href=\"javascript:void(0);\">"+text+"</a>");
			addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent event) {
					mainTabModel.setSelectedTab(tab);
					EventManagerImpl.getInstance().fireEvent(new CreateObjectEvent(modelObjectToCreate));
					dashletModel.setVisible(false);
				}
			});
		}
	}
	public static class DashletModel {
		private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
				this);

		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			propertyChangeSupport.registerPropertyChangeListener(propertyName,
					listener);
		}
		
		private boolean visible;

		public boolean isVisible() {
			return visible;
		}

		public void setVisible(boolean visible) {
			boolean oldVisible = this.visible;
			this.visible = visible;
			propertyChangeSupport.firePropertyChange("visible", oldVisible, visible);
		}
	}

	private DashletController dashletController = new DashletController();
	private final DashletModel dashletModel;
	private final SearchModel searchModel;
	private final MainTabModel mainTabModel;

	public DashletWidget(DashletModel dashletModel, SearchModel searchModel, MainTabModel mainTabModel) {
		this.dashletModel = dashletModel;
		this.searchModel = searchModel;
		this.mainTabModel = mainTabModel;
		setStyleName(XelavosResources.INSTANCE.css().dashletWidget());
	}

	@Override
	public Widget createContentWidget() {
		FlowPanelExt mainPanel = new FlowPanelExt();
		SimplePanelExt overlayPanel = new SimplePanelExt();
		FlexTable flexTable = new FlexTable();
		HTMLExt exitToSiteLink = new HTMLExt("<a href=\"javascript:void(0);\">ir a la pagina</a>");
		
		RoundBorderModel exitToSiteRoundBorderModel = new RoundBorderModel();
		exitToSiteRoundBorderModel.setContentWidget(exitToSiteLink);
		exitToSiteRoundBorderModel.setCssResource(RoundBorderResources.Util.INSTANCE.cssRed());
		RoundBorder exitToSiteRoundBorder = new RoundBorder(exitToSiteRoundBorderModel);
		
		SearchLink searchCompuLink = new SearchLink("la busco", "Computadora", MainTabModel.TAB_ID_SHOPPING,
				new Sort("rating", false));
		SearchLink searchServiceLink = new SearchLink("lo busco", "Colegio", MainTabModel.TAB_ID_SERVICES,
				new Sort("rating", false));
		SearchLink searchWorkLink = new SearchLink("lo busco", "Vendedor", MainTabModel.TAB_ID_JOB,
				new Sort("rating", false));
		SearchLink searchLeisureLink = new SearchLink("lo busco", "Restaurante", MainTabModel.TAB_ID_LEISURE,
				new Sort("rating", false));
		
		Product product = new Product();
		product.setName("Computadora");
		CreateObjectLink insertCompuLink = new CreateObjectLink("la quiero publicar", product, MainTabModel.TAB_ID_SHOPPING);
		Service service = new Service();
		service.setType("Colegio");
		CreateObjectLink insertLawyerLink = new CreateObjectLink("lo quiero publicar", service, MainTabModel.TAB_ID_SERVICES);
		Job job = new Job();
		job.setName("Vendedor");
		CreateObjectLink insertWorkLink = new CreateObjectLink("lo quiero publicar", job, MainTabModel.TAB_ID_JOB);
		Leisure leisure = new Leisure();
		leisure.setName("Restaurante");
		CreateObjectLink insertPartyLink = new CreateObjectLink("lo quiero publicar", leisure, MainTabModel.TAB_ID_LEISURE);
		
//		Image imageCompu = new Image("http://images.google.com/images?q=tbn:ep9FkIFhRg_5YM:www.xettcom.com/tienda/images/cq40.jpg");
		Image imageCompu = XelavosImageBundle.Util.getInstance().computerIcon().createImage();
//		Image imageService = new Image("http://www.acgedu.com/international/files/imagemanagermodule/@random46d252fcd9501/ACG_Parnell_College_2_Titoki_Street_outside_w_students_best_shot_Oct_06.jpg");
		Image imageService = XelavosImageBundle.Util.getInstance().graduationIcon().createImage();
//		Image imageWork = new Image("http://www.n24.de/media/_fotos/3panorama/2008_9/mrz_4/080317_2/Job_dpa.jpg");
		Image imageWork = XelavosImageBundle.Util.getInstance().businessmanIcon().createImage();
		Image imageLeisure = XelavosImageBundle.Util.getInstance().chefIcon().createImage();
//		Image imageParty = new Image("http://www.klangderstille.com/cms/images/stories/events/party-ampere550.jpg");

		flexTable.setWidget(0, 0, XelavosImageBundle.Util.getInstance().shoppingIcon().createImage());
		flexTable.setWidget(2, 0, XelavosImageBundle.Util.getInstance().serviceIcon().createImage());
		flexTable.setWidget(4, 0, XelavosImageBundle.Util.getInstance().jobIcon().createImage());
		flexTable.setWidget(6, 0, XelavosImageBundle.Util.getInstance().leisureIcon().createImage());
		
		flexTable.setWidget(0, 1, new HTML("<strong>Compras</strong> : "));
		flexTable.setWidget(2, 1, new HTML("<strong>Servicios</strong> : "));
		flexTable.setWidget(4, 1, new HTML("<strong>Trabajos</strong> : "));
		flexTable.setWidget(6, 1, new HTML("<strong>Recreaciones</strong> : "));
		
		flexTable.setWidget(0, 2, imageCompu);
		flexTable.setWidget(2, 2, imageService);
		flexTable.setWidget(4, 2, imageWork);
		flexTable.setWidget(6, 2, imageLeisure);
		
		flexTable.setWidget(0, 3, new HTML("La mejor <strong>compu</strong> en Xela?"));
		flexTable.setWidget(2, 3, new HTML("El mejor <strong>colegio</strong> en Xela?"));
		flexTable.setWidget(4, 3, new HTML("El mejor <strong>trabajo en ventas</strong> en Xela?"));
		flexTable.setWidget(6, 3, new HTML("El mejor <strong>restaurante</strong> en Xela?"));
		
		flexTable.setWidget(0, 4, searchCompuLink);
		flexTable.setWidget(2, 4, searchServiceLink);
		flexTable.setWidget(4, 4, searchWorkLink);
		flexTable.setWidget(6, 4, searchLeisureLink);
		
		flexTable.setWidget(0, 5, insertCompuLink);
		flexTable.setWidget(2, 5, insertLawyerLink);
		flexTable.setWidget(4, 5, insertWorkLink);
		flexTable.setWidget(6, 5, insertPartyLink);
		
		HorizontalPanel moreShoppingPanel = new HorizontalPanel();
		FlowPanelExt moreShoppingContentPanel = new FlowPanelExt();
		moreShoppingPanel.add(new InlineHTML("buscar mas compras: "));
		moreShoppingPanel.add(moreShoppingContentPanel);
//		moreShoppingPanel.add(new SearchLink("Impresora", MainTabModel.TAB_ID_SHOPPING, new Sort("registeredDate", false)));
//		moreShoppingPanel.add(new SearchLink("Casa", MainTabModel.TAB_ID_SHOPPING, new Sort("registeredDate", false)));
//		moreShoppingPanel.add(new SearchLink("Lote", MainTabModel.TAB_ID_SHOPPING, new Sort("registeredDate", false)));
//		moreShoppingPanel.add(new SearchLink("Terreno", MainTabModel.TAB_ID_SHOPPING, new Sort("registeredDate", false)));
//		moreShoppingPanel.add(new SearchLink("Carro", MainTabModel.TAB_ID_SHOPPING, new Sort("registeredDate", false)));
//		moreShoppingPanel.add(new SearchLink("Celular", MainTabModel.TAB_ID_SHOPPING, new Sort("registeredDate", false)));
		flexTable.setWidget(1, 1, moreShoppingPanel);
		flexTable.getFlexCellFormatter().setColSpan(1, 1, 4);
		
		HorizontalPanel moreServicesPanel = new HorizontalPanel();
		FlowPanelExt moreServicesContentPanel = new FlowPanelExt();
		moreServicesPanel.add(new InlineHTML("buscar mas servicios: "));
		moreServicesPanel.add(moreServicesContentPanel);
//		moreServicesPanel.add(new SearchLink("Abogado", MainTabModel.TAB_ID_SERVICES, new Sort("registeredDate", false)));
//		moreServicesPanel.add(new SearchLink("Arquitecto", MainTabModel.TAB_ID_SERVICES, new Sort("registeredDate", false)));
//		moreServicesPanel.add(new SearchLink("Universidad", MainTabModel.TAB_ID_SERVICES, new Sort("registeredDate", false)));
//		moreServicesPanel.add(new SearchLink("Lavanderia", MainTabModel.TAB_ID_SERVICES, new Sort("registeredDate", false)));
//		moreServicesPanel.add(new SearchLink("Pediatra", "Pediatr", MainTabModel.TAB_ID_SERVICES, new Sort("registeredDate", false)));
//		moreServicesPanel.add(new SearchLink("Cirujano", "Cirujan", MainTabModel.TAB_ID_SERVICES, new Sort("registeredDate", false)));
		flexTable.setWidget(3, 1, moreServicesPanel);
		flexTable.getFlexCellFormatter().setColSpan(3, 1, 4);
		
		HorizontalPanel moreJobsPanel = new HorizontalPanel();
		FlowPanelExt moreJobsContentPanel = new FlowPanelExt();
		moreJobsPanel.add(new InlineHTML("buscar mas trabajos: "));
		moreJobsPanel.add(moreJobsContentPanel);
//		moreJobsPanel.add(new SearchLink("Maestro", "Maestr", MainTabModel.TAB_ID_JOB, new Sort("registeredDate", false)));
//		moreJobsPanel.add(new SearchLink("Perito contador", MainTabModel.TAB_ID_JOB, new Sort("registeredDate", false)));
//		moreJobsPanel.add(new SearchLink("Mesero", "Meser", MainTabModel.TAB_ID_JOB, new Sort("registeredDate", false)));
//		moreJobsPanel.add(new SearchLink("Administrador", MainTabModel.TAB_ID_JOB, new Sort("registeredDate", false)));
//		moreJobsPanel.add(new SearchLink("Asesor", MainTabModel.TAB_ID_JOB, new Sort("registeredDate", false)));
//		moreJobsPanel.add(new SearchLink("Auxiliar", MainTabModel.TAB_ID_JOB, new Sort("registeredDate", false)));
		flexTable.setWidget(5, 1, moreJobsPanel);
		flexTable.getFlexCellFormatter().setColSpan(5, 1, 4);
		
		HorizontalPanel moreLeisurePanel = new HorizontalPanel();
		FlowPanelExt moreLeisureContentPanel = new FlowPanelExt();
		moreLeisurePanel.add(new InlineHTML("buscar mas recreaciones: "));
		moreLeisurePanel.add(moreLeisureContentPanel);
//		moreLeisurePanel.add(new SearchLink("Concierto", MainTabModel.TAB_ID_LEISURE, new Sort("registeredDate", false)));
//		moreLeisurePanel.add(new SearchLink("Festival", MainTabModel.TAB_ID_LEISURE, new Sort("registeredDate", false)));
//		moreLeisurePanel.add(new SearchLink("Seminario", MainTabModel.TAB_ID_LEISURE, new Sort("registeredDate", false)));
//		moreLeisurePanel.add(new SearchLink("Conferencia", MainTabModel.TAB_ID_LEISURE, new Sort("registeredDate", false)));
//		moreLeisurePanel.add(new SearchLink("Torneo", MainTabModel.TAB_ID_LEISURE, new Sort("registeredDate", false)));
//		moreLeisurePanel.add(new SearchLink("Exposicion", MainTabModel.TAB_ID_LEISURE, new Sort("registeredDate", false)));
		flexTable.setWidget(7, 1, moreLeisurePanel);
		flexTable.getFlexCellFormatter().setColSpan(7, 1, 4);
		
		mainPanel.add(new Image("xelavos_beta.png"));
		mainPanel.add(flexTable);
		mainPanel.add((Widget)exitToSiteRoundBorder);

		RootPanel.get().add(overlayPanel);
		
		overlayPanel.addStyleName(XelavosResources.INSTANCE.css().overlayPanel());
		exitToSiteRoundBorderModel.setStyleNames(XelavosResources.INSTANCE.css().exitToSiteLink());

		dashletController.registerDashletWidget(this);
		dashletController.registerJsUtil(JsUtil.getInstance());
		dashletController.registerEventManager(EventManagerImpl.getInstance());
		dashletController.registerMainTabModel(mainTabModel);
		dashletController.registerExitToSiteLink(exitToSiteLink);
		dashletController.registerDashletModel(dashletModel);
		dashletController.registerSearchModel(searchModel);
		dashletController.registerOverlayPanel(overlayPanel);
		dashletController.registerMoreContentPanel(moreJobsContentPanel, Job.class.getName());
		dashletController.registerMoreContentPanel(moreServicesContentPanel, Service.class.getName());
		dashletController.registerMoreContentPanel(moreShoppingContentPanel, Product.class.getName());
		dashletController.registerMoreContentPanel(moreLeisureContentPanel, Leisure.class.getName());
		
		return mainPanel;
	}

}